//
//  NativeBackNativeChannel.m
//  kq_flutter_widgets
//
//  Created by wtb on 2023/4/7.
//

#import "NativeBackNativeChannel.h"
#import "KqTool.h"

@implementation NativeBackNativeChannel

+(void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar{
    FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:@"flutter.keqiang.com/navigation" binaryMessenger:[registrar messenger]];
    NativeBackNativeChannel *back = [[NativeBackNativeChannel alloc] init];
    [registrar addMethodCallDelegate:back channel:channel];
}

-(void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result{
    Class urlClass = NSClassFromString(@"GFBackManager");
    if (urlClass) {
        //存在
        id vc = [[urlClass alloc] init];
        [vc handleMethodCall:call result:result];
        return;
    }
    if ([@"close" isEqualToString:call.method]){
        [KqTool goBack];
    }else if([@"exitLogin" isEqualToString:call.method]){
        //退出登录
        [KqTool exitLogin];
    }else if([@"setOrientationMode" isEqualToString:call.method]){
        // 屏幕旋转
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        NSNumber *code = call.arguments[@"orientationMode"];
        [dic setValue:code forKey:@"orient"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"flutterPageOrientation" object:nil userInfo:dic];
    }else if([@"goAppStore" isEqualToString:call.method]){
        // 跳转appstore
        [KqTool goAppStore:call.arguments[@"ituneId"]];
        
    }else if([@"open" isEqualToString:call.method]){
        NSString *pageName = call.arguments[@"pageName"];
        NSDictionary *params = call.arguments[@"params"];
        if (pageName && [@"BLE" isEqualToString:pageName]) {
            //跳转到蓝牙
            if ([params objectForKey:@"stationCode"]) {
                [KqTool kqPushWithUrl:@"GFBLEViewController" property:@{@"bleId":[params objectForKey:@"stationCode"]}];
            }
        } else if (pageName && [@"3D_layout" isEqualToString:pageName]) {
            //3d
            //[KqTool setUntiyRootController:params];
            NSString *factoryId = params[@"factoryId"];
            NSString *factoryName = params[@"factoryName"];
            NSString *token = params[@"token"];
            NSString *serverIp = params[@"serverIp"];
            [KqTool kqPushWithUrl:@"GFUnityViewController" property:@{
                 @"factoryId": factoryId != nil ? factoryId : @"",
                 @"factoryName":factoryName != nil ? factoryName : @"",
                 @"token": token != nil ? token : @"",
                 @"serverIp": serverIp != nil ? serverIp : @""}];
        } else if([pageName isEqualToString:@"countryAreaPage"]){
            id areaCallback = ^(NSString * _Nonnull code) {
                if (code) {
                    result(code);
                }
            };
            NSMutableDictionary *data = [NSMutableDictionary dictionary];
            [data setValue:areaCallback forKey:@"areaCallback"];
            [KqTool kqPushWithUrl:@"GFAreaViewController" property:data];
        }else if([pageName isEqualToString:@"shareWX"]){
            Class class = NSClassFromString(@"FlutterCallNativeFuncTool");
            NSObject *call = [[class alloc] init];
            if ([call respondsToSelector:@selector(shareWebToWx:)]) {
                [call performSelector:@selector(shareWebToWx:) withObject:params];
            }
        }else if([pageName isEqualToString:@"shareDD"]){
            Class class = NSClassFromString(@"FlutterCallNativeFuncTool");
            NSObject *call = [[class alloc] init];
            if ([call respondsToSelector:@selector(shareWebToDT:)]) {
                [call performSelector:@selector(shareWebToDT:) withObject:params];
            }
        }else if([pageName isEqualToString:@"CloudSchool"]){
            //云学堂-小诸葛
            [KqTool kqPushWithUrl:@"GFH5ViewController" property:@{@"h5Type":@"1",@"isWebTitle":@1,@"isBehavior":@1,@"key":[params objectForKey:@"key"],@"language":[params objectForKey:@"language"],@"ip":[params objectForKey:@"ip"]}];
        }else if([pageName isEqualToString:@"XzgPrice"]){
            //报价器-小诸葛
            NSString *url = [params objectForKey:@"url"];
            NSString *token = [params objectForKey:@"token"];
            NSString *phone = [params objectForKey:@"phone"];
            [KqTool kqPushWithUrl:@"GFH5ViewController" property:@{@"url":url,@"isWebTitle":@1,@"isBehavior":@1,@"token":token ? token : @"",@"phone": phone ? phone : @""}];

        }else if([pageName isEqualToString:@"scanHelpPage"]){
            //扫码帮助界面
            [KqTool kqPushWithUrl:@"HelpViewController" property:nil];
        }else if([pageName isEqualToString:@"message"]){
            //管工厂云消息中心
            [KqTool kqPushWithUrl:@"GFMsgCenterViewController" property:nil];
        }else if([pageName isEqualToString:@"nativeHome"]){
            [KqTool resetRootViewController];
        }
    }
}

@end
